<?php

namespace App\Service\Content;

use App\Constants\ErrorCode;
use App\Constants\Redis\TempRedisCode;
use App\Model\Content\LongTextModel;
use App\Model\Content\NoticeModel;
use App\Service\BaseService;
use Hyperf\Collection\Arr;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class NoticeService extends BaseService
{

    #[Inject]
    protected NoticeModel $noticeModel;

    #[Inject]
    protected LongTextModel $longTextModel;

    public function noticePageListForAdmin($params=[], $page=1, $ps=10)
    {
        return $this->noticeModel->pageListForAdmin($params, $page, $ps)->toArray();
    }

    public function addNotice($params, $adminId=0)
    {
        $lock = TempRedisCode::addNoticeLock($adminId);
        $this->lockError($lock);

        Db::beginTransaction();
        try {
            $params['long_text_id'] = $this->longTextModel->addOneGetId([
                'content' => $params['content'],
            ]);
            if (!$params['long_text_id']){
                throw new \Exception('保存内容失败',ErrorCode::SERVICE_HAND_FAILED);
            }

            $r = $this->noticeModel->addOne($params);
            if (!$r){
                throw new \Exception('保存失败',ErrorCode::SERVICE_HAND_FAILED);
            }
            Db::commit();
            $this->freeLock($lock);
            return [true,'保存成功'];
        }catch (\Throwable $e){

            Db::rollBack();
            $this->freeLock($lock);
            return $this->errorMsg($e,'保存失败');
        }
    }

    public function noticeDetailForAdmin($id)
    {
        $notice = $this->noticeModel->findById($id,[
            'id',
            'sort',
            'show_status',
            'title',
            'long_text_id',
        ])?->toArray();
        if (!$notice){
            return [false,'数据不存在'];
        }
        $content = $this->longTextModel->findById($notice['long_text_id'],['content']);
        $notice['content'] = $content ? $content->content : '';
        return [true, $notice];
    }

    public function updateNotice($id, $data)
    {
        $notice = $this->noticeModel->findById($id,['id']);
        if (!$notice){
            return [false,'数据不存在'];
        }
        Db::beginTransaction();
        try {
            $r = $this->noticeModel->updateById($id, $data);
            $r2 = $this->longTextModel->updateById($id, Arr::only($data,['content']));
            if (!$r && !$r2){
                throw new \Exception('没有改变',ErrorCode::SERVICE_HAND_FAILED);
            }
            Db::commit();
            return [true,'更新成功'];
        }catch (\Throwable $e){
            Db::rollBack();
            return $this->errorMsg($e,'更新失败');
        }
    }

    public function delNotice($ids)
    {
        $notices = $this->noticeModel->getSomeByIds($ids,['id','long_text_id']);
        if ($notices->isEmpty()){
            return [false,'数据不存在或已删除'];
        }
        Db::beginTransaction();
        try {
            $r = $this->noticeModel->delByIds($ids);
            if (!$r){
                throw new \Exception('删除失败',ErrorCode::SERVICE_HAND_FAILED);
            }
            $r2 = $this->longTextModel->delByIds($notices->pluck('long_text_id')->toArray());
            if (!$r2){
                throw new \Exception('删除失败',ErrorCode::SERVICE_HAND_FAILED);
            }
            Db::commit();
            return [true,'删除成功'];
        }catch (\Throwable $e){
            Db::rollBack();
            return $this->errorMsg($e,'更新失败');
        }
    }

}